package com.eden.door.component;  

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eden.door.constant.Constant_Sys;
import com.eden.door.context.AppContext;
import com.eden.door.entity.Door;
import com.eden.door.entity.DoorAttr;
import com.eden.door.util.DoorUtil;
import com.eden.door.view.DoorView;
import com.eden.door.view.MessageBox;

/**
 * 计算按钮
 * @author Eden
 * @date 
 * @version
 * @midify
 */
public class CalculateButton extends JButton implements ActionListener{

	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(CalculateButton.class) ;
	
	public CalculateButton(String name){
		super(name) ;
		this.addActionListener(this) ;
	}
	
	/**
	 * 计算按钮的监听器
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
		log.debug("取出当前显示的门页面") ;
		DoorView doorView = AppContext.instance().getCurrDoorView();
		Door door = doorView.getDoorWithValue();
		
		if(log.isDebugEnabled())
			log.debug("计算之前的属性的值：" + DoorUtil.showDoorAttrValue(door.getDoorAttrs())) ;
		
		//根据门的各属性的值,公式进行计算
		try{
		door = DoorUtil.calculate(door) ;
		} catch(Exception e1) {
			MessageBox.showMsgBox("输入有误!") ;
			log.warn("计算时发生错误", e1) ;
		}
		
		if(log.isDebugEnabled())
			log.debug("按照配置文件的公式计算门的各属性的值，计算结果为：" + DoorUtil.showDoorAttrValue(door.getDoorAttrs())) ;
		doorView.getDoorAttrJcompMap() ;
		
		Map<String,DoorAttr> doorAttrs = door.getDoorAttrs();
		Map<String , JComponent> doorAttrJcompMap = doorView.getDoorAttrJcompMap();
		String code = "" ;
		DoorAttr doorAttr = null;
		JComponent jComp = null ;
		for(Iterator<Entry<String,DoorAttr>> i = doorAttrs.entrySet().iterator() ; i.hasNext();){
			Entry<String,DoorAttr> entry = i.next() ;
			
			code = entry.getKey();
			doorAttr = entry.getValue();
			
			//数值显示格式化,显示小数点后两位
			DecimalFormat format = new DecimalFormat(".##") ;
			
			//如果属性的填充类型为自动时
			if(StringUtils.equals(Constant_Sys.DOOR_ATTR_FILL_AUTO, doorAttr.getFill())){
				jComp = doorAttrJcompMap.get(code) ;
				if(StringUtils.equals(Constant_Sys.DOOR_ATTR_TYPE_TEXT , doorAttr.getType())){
					String value = format.format(Double.valueOf(doorAttr.getValue())) ;
					//设置文本框中显示
					((JTextField)(jComp)).setText(value) ;
				} else if(StringUtils.equals(Constant_Sys.DOOR_ATTR_TYPE_SELECT, doorAttr.getType())){
					//设置选中的条目
					((JComboBox)jComp).setSelectedItem(doorAttr.getValue()) ; 
				}
			}
		}
	}
	
	/**
	 * 计算之前验证输入是否合法
	 * @return 合法返回true ,否则返回false
	 */
	public boolean check(Door door){
		Map<String, DoorAttr> doorAttrMap = door.getDoorAttrs() ;
		DoorAttr doorAttr ;
		Object value ;
		for(Iterator<Entry<String , DoorAttr>> i = doorAttrMap.entrySet().iterator() ; i.hasNext() ;){
			doorAttr = i.next().getValue() ;
			value = doorAttr.getValue() ;
			if(value == null) {
				
			}
		}
		
		return false ;
	}
	
}
